package com.carlosli;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;

import java.io.IOException;

public class SolrApiTest {
    public static void main(String[] args) {
        SolrApiTest test = new SolrApiTest();
        test.indexTest();
        test.indexTestWithMultiThread();
        test.queryTest();
        test.deleteTest();
        test.queryTest();
    }

    /**
     * 查询索引
     */
    public void queryTest() {
        // 组装请求地址，这里具体到core的名字
        String urlString = Constant.SOLR_SERVER_ADDRESS_AND_PORT + "solr/product";
        SolrServer server = new HttpSolrServer(urlString);

        // 查询条件
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.setSort(new SolrQuery.SortClause("id", SolrQuery.ORDER.asc));

        QueryResponse response = null;
        try {
            response = server.query(query);
            server.shutdown();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }

        SolrDocumentList list = response.getResults();
        if (list.size() == 0) System.out.println("result is empty!");
        for (SolrDocument entries : list) {
            System.out.println("id:" + entries.getFieldValue("id"));
//            System.out.println(entries.getFieldNames());
        }
    }

    /**
     * 建立索引
     */
    public void indexTest() {
        // 组装请求地址，这里具体到core的名字
        String urlString = Constant.SOLR_SERVER_ADDRESS_AND_PORT + "solr/product";
        SolrServer server = new HttpSolrServer(urlString);

        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", "111");
        document.addField("productName", "水杯");
        document.addField("price", "49.99");
        try {
            UpdateResponse response = server.add(document);
            server.commit();
            server.shutdown();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 多线程队列建立索引
     */
    public void indexTestWithMultiThread() {
        // 组装请求地址，这里具体到core的名字
        String urlString = Constant.SOLR_SERVER_ADDRESS_AND_PORT + "solr/product";
        ConcurrentUpdateSolrServer server = new ConcurrentUpdateSolrServer(urlString, 10, 2);

        try {
            for (int i = 0; i < 5; i++) {
                SolrInputDocument document = new SolrInputDocument();
                document.addField("id", "55" + i);
                document.addField("productName", "水杯" + i);
                document.addField("price", "49.99");
                server.add(document);
            }

            server.commit();
            server.shutdown();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除索引
     */
    public void deleteTest() {
        // 组装请求地址，这里具体到core的名字
        String urlString = Constant.SOLR_SERVER_ADDRESS_AND_PORT + "solr/product";
        HttpSolrServer server = new HttpSolrServer(urlString);
        try {
            server.deleteByQuery("*:*");
            server.commit();
            server.shutdown();
            System.out.println("everything was gone!");
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
